import { Component, OnInit } from '@angular/core';
import { PaginationModel } from 'app/widgets/pagination';
import { environment } from '../../../../environments/environment';
import { Table1Service } from "./table1.service";
import * as flatpickr from 'flatpickr';
import { zh } from "flatpickr/dist/l10n/zh.js";
import * as _ from 'lodash';
import * as moment from 'moment';
import { Util } from 'app/common/util';
import { DataService } from '../../../services/data.service';

@Component({
  selector: 'app-table1',
  templateUrl: './table1.component.html',
  styleUrls: ['./table1.component.scss'],
  providers: [Table1Service]
})
export class Table1Component implements OnInit {
  // lodash
  public _: any = _;
  // 菜单数据
  public menuData: any;
  // 菜单按钮集合
  public menuResourceSet: any;
  // 字典值
  public dictionaryList: any = {
    FUND_TYPE: []
  };
  // 分页列表post
  public getBody = {
    pageIndex: 1,
    pageSize: 10,
    sBegin: '',
    sEnd: '',
    sFundType: '',
    sIncludeYzhzh: '1',
    nFundNoList: ''
  };
  // 分页配置
  public listPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  // 分页列表数据
  public dataList: any;
  // 列表全选
  public isCheckedAll: any = false;

  constructor(
    public table1Service: Table1Service,
    public dataservice: DataService
  ) {

  }
  ngOnInit() {
    let that = this;
    // 权限处理
    this.dataservice.initTree.subscribe((data) => {
      let SessionMenuData = sessionStorage.getItem('menu');
      this.menuData = JSON.parse(SessionMenuData)
      this.menuResourceSet = [];
      _.forEach(this.menuData, (item, index) => {
        if (item.sName == '数据统计') {
          _.forEach(item.children, (it, idx) => {
            if (it.sName == '分产品资金费用表') {
              that.menuResourceSet = it.resourceSet
              // sessionStorage.setItem('btn-otm', that.menuResourceSet);
            }
          })
        }
      })
    })
    let SessionMenuData = sessionStorage.getItem('menu');
    this.menuData = JSON.parse(SessionMenuData)
    this.menuResourceSet = [];
    _.forEach(this.menuData, (item, index) => {
      if (item.sName == '数据统计') {
        _.forEach(item.children, (it, idx) => {
          if (it.sName == '分产品资金费用表') {
            that.menuResourceSet = it.resourceSet
            // sessionStorage.setItem('btn-otm', that.menuResourceSet);
          }
        })
      }
    })
    // 初始化日期控件
    flatpickr.localize(zh);
    // 初始化默认日期
    this.getBody.sEnd = moment().format('YYYY-MM-DD');
    this.getBody.sBegin = moment().format('YYYY') + '-01-01';
    // 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
    this.list();
  }
  // 分页列表
  list(pageNum?, pageSize?) {
    let that = this;
    if (Util.dateOrder(this.getBody.sBegin, this.getBody.sEnd)) {
      this.isCheckedAll = false;
      this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
      this.getBody.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
      this.table1Service.list(this.getBody).subscribe(data => {
        if (data) {
          that.dataList = data.list;
          that.listPageInfo.currentPageNum = data.pageNum;
          that.listPageInfo.totalPages = data.pages;
          that.listPageInfo.total = data.total;
          that.listPageInfo.startRow = data.startRow;
          that.listPageInfo.endRow = data.endRow;
        }
        // 给表格列支持列拖动和固定表头效果
        Util.colResizable("#t1List");
      });
    } else {
      window['swal']("提示", "开始日期 须早于 结束日期", "warning");
    }
    
  }
  //个数分页
  listPageSizeChange(pageSize: number) {
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      this.list();
    }
  }
  // 条数分页
  listPageNavigation(currentPageNum: number) {
    this.listPageInfo.currentPageNum = currentPageNum;
    this.list(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
  }
  // 列表全选
  checkedAll() {
    if (this.isCheckedAll) {
      _.forEach(this.dataList, item => {
        item.isChecked = true;
      });
    } else {
      _.forEach(this.dataList, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.dataList, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.dataList.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }
  // 导出
  exportExcel() {
    let that = this;
    // let checkedItem = [];
    // _.forEach(that.dataList, item => {
    //   if (item.isChecked) {
    //     checkedItem.push(item.nFundNo);
    //   }
    // })
    // let converItem: any = checkedItem.join(',');
    let getBody = {
      "nFundNoList": '',
      "sBegin": this.getBody.sBegin,
      "sEnd": this.getBody.sEnd,
      "sFundType": this.getBody.sFundType,
      'sIncludeYzhzh': this.getBody.sIncludeYzhzh
    }
    const iframe = window['$']("<iframe id='downloadiframe'>");
    iframe.attr('style', 'display:none');
    //  转码，兼容ie
    // getBody.sFundName = encodeURI(getBody.sFundName);
    iframe.attr('src', environment.server + '/pms/v1/reportForm/export/reportForm2?token=' + sessionStorage.getItem('token') + '&nFundNoList=' + getBody.nFundNoList + '&sBegin=' + getBody.sBegin + '&sEnd=' + getBody.sEnd + '&sFundType=' + getBody.sFundType + '&sIncludeYzhzh=' + getBody.sIncludeYzhzh);
    window['$']('body').append(iframe);
    setTimeout("$('#downloadiframe').remove()", 60000);
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.dataList, [colName], ["asc"]);
    if (JSON.stringify(this.dataList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.dataList, [colName], ["desc"]);
    }
    this.dataList = listDataAsc;
  }
}